﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Cache
{
	public class SystemMapCache
	{
		public static readonly SystemMapCache Instance = new SystemMapCache();
		private SystemMapCache()
		{}
		private object SyncRoot = new object();
		private IDictionary<string, TB_SystemMap> _CacheDictionary = null;
		private IDictionary<string, TB_SystemMap> CacheDictionary // = new Dictionary<string, TB_SystemMap>();
		{
			get
			{
				if(_CacheDictionary == null || _CacheDictionary.Count == 0)
				{
					ReLoadFromDB();
				}
				return _CacheDictionary;
			}
		}
		public IList<TB_SystemMap> List
		{
			get
			{
				IList<TB_SystemMap> list = new List<TB_SystemMap>();
				lock (SyncRoot)
				{
					if (CacheDictionary != null)
					{
						list = CacheDictionary.Values.ToList<TB_SystemMap>();
					}
				}
				return list;
			}
		}

		private void ReLoadFromDB()
		{
			Expression<Func<TB_SystemMap, bool>> f = (p => true);
			Expression<Func<TB_SystemMap, string>> o = (p => p.Code);
			var systemMapList = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f, o, true).OrderBy(p => p.Code).ThenBy(p => p.SortIndex);
			lock(SyncRoot)
			{
				_CacheDictionary = new Dictionary<string, TB_SystemMap>();
				foreach(var item in systemMapList)
				{
					if(_CacheDictionary.ContainsKey(item.Code))
					{
						_CacheDictionary[item.Code] = item;
					}
					else
					{
						_CacheDictionary.Add(item.Code, item);
					}
				}
			}
		}
		
		public IList<TB_SystemMap> GetParent(string code)
		{
			return List.Where(p => code.StartsWith(p.Code)).ToList();
		}
		/*
		public IList<TB_SystemMap> GetChildren(string code)
		{
			return List.Where(p => p.Code.StartsWith(code) && p.Code != code && p.IsAdminRight).ToList();
		}

		public bool HasChildren(TB_SystemMap map)
		{
			return List.Where(p => p.Code.StartsWith(map.Code) && p.Code != map.Code && p.IsAdminRight).Count() > 0;
		}*/

		public static IList<dynamic> JsonList(IList<TB_SystemMap> list)
		{
			return list.Select(p => new { Code = p.Code, Name = p.Name, CText = p.CText, CURL = p.CURL, Description = p.Description, EText = p.EText, EURL = p.EURL, IsAdminRight = p.IsAdminRight, ShowInMap = p.ShowInMap, SortIndex = p.SortIndex, Status = p.Status, AdminURL = p.AdminURL, OpenInNewWindow = p.OpenInNewWindow, NeedLogin = p.NeedMemberLogin }).ToList<dynamic>();
		}

		public TB_SystemMap this[string key]
		{
			get
			{
				TB_SystemMap retObj = null;
				lock (SyncRoot)
				{
					if(CacheDictionary.ContainsKey(key))
					{
						retObj = CacheDictionary[key];
					}
				}
				return retObj;
			}
		}

		public void Add(TB_SystemMap systemMap)
		{
			lock (SyncRoot)
			{
				if (CacheDictionary.ContainsKey(systemMap.Code))
				{
					CacheDictionary[systemMap.Code] = systemMap;
				}
				else
				{
					CacheDictionary.Add(systemMap.Code, systemMap);
				}
			}
		}

		public void Remove(TB_SystemMap systemMap)
		{
			Remove(systemMap.Code);
		}

		public void Remove(string code)
		{
			lock(SyncRoot)
			{
				CacheDictionary.Remove(code);
			}
		}

		public void Clear()
		{
			lock(SyncRoot)
			{
				CacheDictionary.Clear();
			}
		}
	}
}